<?php

/*
 * 
 * 上下限阀值设置
 * 
 */

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\PriDeviceInfo;
use App\Models\Deviceparam;

use App\Models\Devicecontrol;
use App\Models\Devicecontrolparam;
use App\Models\ControlConfig;
use App\Models\WarnValueset;
use DB;
use Validator;
// use Illuminate\Validation\Validator;

/**
 * 上下限阀值设置
 *
 * @author litc
 */
class LimitController extends Controller {

    /**
     * 获取设备参数`
     * @param Request $request
     * @return array
     */
    public function index(Request $request) {

        $perPage = $request->input('pageSize', 15);

        $pdiIndex = $request->input('pdi_index');

        $query = WarnValueset::query();

        if($pdiIndex) {
            $query->where('v_pid', $pdiIndex);
        }

        $query->select(['v_id', 'v_pid', 'v_warnname', 'v_warnnameerro', 'v_warnvalue_up', 'v_warnvalue_down', 'pdi_name', 'v_updatetime']);

        $query->join('t_prideviceinfo', 'pdi_index', '=', 'v_pid');

        $info = $query->paginate($perPage);

        return [ 'data'=>$info ];
    }

    //计算deviceparam dp_id
    private function getCommandId($dpId, $dcmup = 6) {
        return $dpId - 65536 * $dcmup;
    }

    /**
     * 保存命令
     * @param Request $request
     * @param type $pdi
     * @return type
     */
    public function saveCommand(Request $request) {

        $rules = [
            'data.*.v_warnvalue_up' => 'required|numeric',
            'data.*.v_warnvalue_down' => 'required|numeric',
            'data.*.v_id' => 'required|numeric',
            'data.*.v_pid' => 'required|exists:t_prideviceinfo,pdi_index',
        ];

        $message = [
            'data.*.v_warnvalue_up.required'=>'上限阈值不能为空',
            'data.*.v_warnvalue_up.numeric'=>'上限阈值必须是数字',
            // 'data.*.v_warnvalue_up.gt'=>'上限阀值必须大于下限阀值',
            'data.*.v_warnvalue_down.required'=>'下限阈值不能为空',
            'data.*.v_warnvalue_down.numeric'=>'下限阈值必须是数字',

            'data.*.v_id.required'=>'阈值编号不能为空',
            'data.*.v_id.numeric'=>'阈值编号必须是数字',

            'data.*.v_pid.required'=>'设备编号不能为空',
            'data.*.v_pid.exists'=>'设备不存在',
        ];

        $inputs = $request->input();

        $validator = Validator::make($inputs, $rules, $message);

        $validator->after(function ($validator)use($inputs) {
            $flag = false;
            $data = array_get($inputs, 'data', []);
            foreach($data as $k=>$item) {
                $up = data_get($item, 'v_warnvalue_up');
                $down = data_get($item, 'v_warnvalue_down');
                $name = data_get($item, 'v_warnnameerro');
                if($up<=$down) {
                    $validator->errors()->add('data.*.v_warnvalue_up', $name.'上限阀值必须大于下限阀值');
                    break;
                }
            }
        });

        $validator->validate();

       // $this->validate($request, $rules, $message);

        list($err, $msg) = $this->toDatabase($request);

        if($err) {
            $this->error($msg);
        }
    }

    //保存配置参数到库
    protected function toDatabase($request) {
        $errors = [];
        $data = $request->input('data');
        $total = count($data);
        if($total==0) {
            return [false, ''];
        }

        DB::transaction(function () use($data, &$errors) {
            foreach($data as $item) {
                $updated = WarnValueset::where('v_pid', array_get($item, 'v_pid'))
                            ->where('v_id', array_get($item, 'v_id'))
                            ->update([
                                'v_warnvalue_up'=>array_get($item, 'v_warnvalue_up'),
                                'v_warnvalue_down'=>array_get($item, 'v_warnvalue_down'),
                                'v_updatetime'=>date('Y-m-d H:i:s')
                            ]);
                if($updated===false){
                    $errors[] = array_get($item, 'v_warnnameerro');
                }
            }
        });

        $failNum = count($errors);
        $success = '保存成功';
        $fail = implode(', ', $errors).'保存失败';
        return [$failNum>0,  $fail];
    }
    
}
